/**
 * @class ExtGames.Deck
 * @param {Array} cards An array of cards to initialise the deck with
 * Represents the deck of all non-dealt cards
 */
ExtGames.DiscardPile = function(config) {
  var config = config || {};
  
  ExtGames.DiscardPile.superclass.constructor.call(this, config);
      
  this.addEvents(
		    /**
		     * @event carddropped
		     * Fires when a card is dropped on this stack
		     * @param {ExtGames.Stack} this The stack the card was dropped on
		     * @param {ExtGames.Card} card The card that was dropped
		     */
		    'carddropped'
		  );
  
  this.on('render',      this.initializeDropTarget, this);
  this.on('afterlayout', this.applyClasses, this);
};

Ext.extend(ExtGames.DiscardPile, ExtGames.CardHolder, {
  
  initTopCard: function(topcard) {
	  this.add(topcard);
	  topcard.revealed = true;
  },
  
  
  dropAllowed: function(cardToDrop) {
  	  
	if (cardToDrop.location.itemId && cardToDrop.location.itemId == 'hand') {
		return true;
	}
	  
	return false;
  },
  
  /**
   * @property numberToDeal
   * @type Number
   * The number of cards to deal each time the deck is clicked (usually 1 or 3, defaults to 1)
   */
  numberToDeal: 1,
  
  /**
   * Renders the Deck
   * @param {Ext.Container} ct The container to render this component to
   */
  onRender: function(ct, position) {
    this.el = Ext.get(ct).createChild({
      cls: 'x-solitaire-discard-wrapper',
      children: [{
			tag: 'h1',
			html: 'Discard'
		},{
        cls:   'x-solitaire-discard',
        style: 'background-image: url('+cardUrl+');'
      }]
    });
    
    this.deckHolder = this.el.child('.x-solitaire-discard');
    
    ExtGames.DiscardPile.superclass.onRender.apply(this, arguments);
  },
  
  getLayoutTarget: function(paramName) {
    return this.deckHolder;
  },
  
  /**
   * Iterates over the cards in the deck, adding CSS classes accordingly.
   * Intended to be attached to the afterlayout event
   */
  applyClasses: function() {
    this.items.each(function(c) {
    	c.removeClass('x-solitaire-card-left');
    	c.removeClass('x-solitaire-card-right');
    	c.removeClass('x-solitaire-unused-top');
    	
      if (c == this.items.items[this.items.length - 1]) {
        //top card in stack
        c.addClass('x-solitaire-card-top');
        c.removeClass('x-solitaire-card-under');
        
      } else {
        c.addClass('x-solitaire-card-under');
        c.removeClass('x-solitaire-card-top');
      };
      
      c.initializeDragSource();
    }, this);
  },
  
  
  initializeDropTarget: function() {
	    //local reference to use in dropTarget config
	    var stack = this;
	    
	    this.dropTarget = new Ext.dd.DropTarget(this.getEl(), {
	      notifyOver: function(source, e, data) {
	        if (stack.dropAllowed(data.card)) {
	          return Ext.dd.DropTarget.prototype.dropAllowed;
	        } else {
	          return Ext.dd.DropTarget.prototype.dropNotAllowed;
	        };
	      },
	      
	      notifyDrop: function(source, e, data) {
	        if (stack.dropAllowed(data.card)) {
	          return stack.fireEvent('carddropped', stack, data.card);
	        } else {
	          return false;
	        }
	      }
	    });
	  },
	  
	getCards: function() {
	  	if (this.items && this.items.items) {
	  		return this.items.items;
	  	}
	  	return [];
  	},
  	
  	getLastCard: function() {
  		if (this.items && this.items.items && this.items.items.length > 0) {
	  		return this.items.items[this.items.items.length-1];
	  	}
  	}
  
  
  
});